package com.bjsxt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**核心业务类*/
//operation:运作，实施
public class Operate {
    private List<Person> list;
    public Operate(){
        this.list = new ArrayList<>();
    }
    //用户添加记录业务逻辑控制
    public void addLogic(){
        //如果运行一下就结束方法，那么用户看到二级子菜单下一秒，就跳回到主菜单
        //所以用无限循环，保留二级菜单
        //实例对象
        Menu menu = new Menu();
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        while (true){
            menu.addMenu();
            int item = telNoteRegex.menuItemValidate(1,3);
            switch (item){
                //因为addOperation()本身体
                //就在Operate 中，用this调用就行
                case 1:this.addOperation();break;
                case 2:this.showAll();break;
                //return结束了while循环，addLogic方法就会
                //跟着结束，就会返回上一级
                case 3:return;
            }
        }

    }
    //用户查询记录业务逻辑控制
    public void searchLogic(){
        Menu menu = new Menu();
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        while (true){
            menu.searchMenu();
            int item = telNoteRegex.menuItemValidate(1,7);
            switch (item){
                case 1:this.searchBYName();break;
                case 2:this.searchByAge();break;
                case 3:this.searchBySex();break;
                case 4:this.searchByTelNum();break;
                case 5:this.searchByAddress();break;
                case 6:this.showAll();break;
                case 7:return;
            }
        }

    }
    //修改记录业务逻辑
    public void modifyLogic(){
        Menu menu = new Menu();
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        while (true){
            menu.modifyMenu();
            int item = telNoteRegex.menuItemValidate(1,3);
            switch (item){
                case 1: this.showAll();break;
                case 2: this.modifyOperation();break;
                case 3: return;
            }
        }

    }
    //删除记录业务逻辑控制
    public void deleteLogic(){
        Menu menu = new Menu();
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        while (true){
            menu.deleteMenu();
            int item = telNoteRegex.menuItemValidate(1,4);
            switch (item){
                case 1:this.showAll();break;
                case 2:this.deleteOperation();break;
                case 3:this.deleteAllOperation();break;
                case 4:return;
            }
        }

    }
    //排序记录业务逻辑控制
    public void orderLogic(){
        Menu menu = new Menu();
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        while (true){
            menu.orderMenu();
            int item = telNoteRegex.menuItemValidate(1,5);
            switch (item){
                case 1:this.orderName();break;
                case 2:this.orderAge();break;
                case 3:this.orderSex();break;
                case 4:this.showAll();
                case 5:return;
            }
        }

    }
    //添加新记录信息
    public void addOperation(){
        //实例化TelNoteRegex，调用里面的方法，能得到正确的数据
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        String name = telNoteRegex.nameValidate();
        String age = telNoteRegex.ageValidate();
        String sex = telNoteRegex.sexValidate();
        String telNum = telNoteRegex.telNumValidate();
        String address = telNoteRegex.addressValidate();
        Person person = new Person(name,age,sex,telNum,address);
        //ArrayList已经在上面实例化
        this.list.add(person);
        //id用list.size()为记录的id
        person.setId(this.list.size()); //size 是1


    }
    //查询全部记录
    public void showAll(){
        //所有元素都放在ArrayList当中，将它们拿出来就行
        //首先判断是否有记录
        if(this.list.size() == 0){
            System.out.println("没有任何记录");
            return;
        }
        for (int i=0;i<this.list.size();i++){
            System.out.println(this.list.get(i));
        }


    }
    //按姓名查询记录
    public void searchBYName(){
        //先取得合法的姓名
       TelNoteRegex telNoteRegex = new TelNoteRegex();
       String name = telNoteRegex.nameValidate();
       //加个开关，方便第二个if的条件描述
       boolean flag = true;
       for(int i=0;i<this.list.size();i++){
           if(name.equals(this.list.get(i).getName())){
               System.out.println(this.list.get(i));
               flag = false; //找到后关闭程序
           }
       }
       if(flag){
           System.out.println("没有此人记录");
       }

    }
    //按年龄查询记录
    public void searchByAge(){
       TelNoteRegex telNoteRegex = new TelNoteRegex();
       String age = telNoteRegex.ageValidate();
       boolean flag = true;
       for(int i=0;i<this.list.size();i++){
           if(age.equals(this.list.get(i).getAge())){
               System.out.println(this.list.get(i));
           }
       }
       if(flag){
           System.out.println("没有此人记录");
       }
    }
    //按性别查询记录
    public void searchBySex(){
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        String sex = telNoteRegex.sexValidate();
        boolean flag =true;
        for(int i=0;i<this.list.size();i++){
            //equalsIgnoreCase忽略大小写
            if(sex.equalsIgnoreCase(this.list.get(i).getSex())){
                System.out.println(this.list.get(i));
                flag =false;
            }
        }
        if(flag){
            System.out.println("没有此人记录");
        }
    }
    //按电话号码查询记录
    public void searchByTelNum(){
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        String telNum = telNoteRegex.telNumValidate();
        boolean flag = true;
        for(int i=0;i<this.list.size();i++){
            if(telNum.equals(this.list.get(i).getTelNum())){
                System.out.println(this.list.get(i));
                flag = false;
            }
        }
        if(flag){
            System.out.println("没有此人记录");
        }
    }
    //按地址查询记录
    public void searchByAddress(){
       TelNoteRegex telNoteRegex = new TelNoteRegex();
       String address = telNoteRegex.addressValidate();
       boolean flag = true;
       for(int i=0;i<this.list.size();i++){
           if(address.equals(this.list.get(i).getAddress())){
               System.out.println(this.list.get(i));
               flag = false;
           }
       }
       if(flag){
           System.out.println("没有记录此人");
       }
    }
    //修改指定记录
    public void  modifyOperation(){
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        Menu menu = new Menu();
        //对被修改的记录序号进行验证，可以使用对菜单项验证的方法来完成
        System.out.println("请输入记录对象");
        //合法的序号
        int itemNum = telNoteRegex.menuItemValidate(1,list.size());
        //打开三级菜单项
        menu.subModifyMenu();
        int menuItem = telNoteRegex.menuItemValidate(1,6);
        switch (menuItem){
            //修改姓名：拿到一个合法的姓名，再放入要修改的对象中，get是从0开始所以序号要-1，
            case 1:String name = telNoteRegex.nameValidate();(this.list.get(itemNum-1)).setName(name);break;
            case 2:String age = telNoteRegex.ageValidate();(this.list.get(itemNum-1)).setAge(age);break;
            case 3:String sex = telNoteRegex.sexValidate();(this.list.get(itemNum-1)).setSex(sex);break;
            case 4:String telNum = telNoteRegex.telNumValidate();(this.list.get(itemNum-1)).setTelNum(telNum);break;
            case 5:String address = telNoteRegex.addressValidate();(this.list.get(itemNum-1)).setAddress(address);break;
            case 6:return;
        }
    }
    //删除指定记录
    public void deleteOperation(){
        TelNoteRegex telNoteRegex = new TelNoteRegex();
        System.out.println("请输入记录序号");
        int itemNum = telNoteRegex.menuItemValidate(1, list.size());
        //因为索引从0开始，所以序号-1
        this.list.remove(itemNum-1);
        //删除后序号不连续，重新输入序号
        for (int i=0;i<this.list.size();i++){
            //遍历每一个元素，再设置序号，用迭代因子i+1，
            (this.list.get(i)).setId(i+1);

        }
        System.out.println("删除成功，请继续操作");
    }
    //删除全部记录
    public void deleteAllOperation(){
        this.list.clear();
        System.out.println("电话本内容已清空，请继续操作");
    }
    //按用户姓名排序记录
    public void orderName(){
    //用Collections类下的sort方法，能对list容器进行排序
        //                 容器         比较器
        Collections.sort(this.list,new OrderByName());
        //排列后顺序乱了，从重新设置序号
        for(int i=0;i<this.list.size();i++){
            //加括号，优先级，先得到元素
            (this.list.get(i)).setId(i+1);
        }

    }
    //按用户年龄排序记录
    public void orderAge(){

        Collections.sort(this.list,new OrderByAge());
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }
    }
    //按用户性别排序记录
    public void orderSex(){
        Collections.sort(this.list,new OrderBySex());
        for(int i=0;i<this.list.size();i++){
            (this.list.get(i)).setId(i+1);
        }

    }

    /*排序器只能对在电话本中所存储的记录进行排序，不具备通用性，所以定义
    * 成Operate 的内部类*/

    //按姓名排序的比较器
    class OrderByName implements Comparator<Person>{
        //implement :执行  实施

        @Override
        public int compare(Person o1, Person o2) {
/*String类实现了compareTo 方法  使用自身的规则实现比较方式
* 所以可以直接用String类下的compareTo方法进行比较*/
            return o1.getName().compareTo(o2.getName());
        }
    }
    //按年龄排序的比较器
    //compareTo对于数字会采取升聚处理小的在前面，大的在后面
    class OrderByAge implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getAge().compareTo(o2.getAge());
        }
    }
    //按性别排序的比较器
    class OrderBySex implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getSex().compareTo(o2.getSex());
        }
    }
}
